package service

import (
	"encoding/json"
	"errors"
	"fmt"
	constant "rongyy/src/constants"
	"rongyy/src/middlewares"
	"rongyy/src/models"
	"strings"
)

type RefundService struct {
}

func (pc *RefundService) RefundFromHis(req *models.RefundHisReq) error {
	if strings.Contains(req.OrderSn, "R") {
		// 挂号退费
		reqHis := &models.RegisterModel{}
		orders, has, err := reqHis.FindByOrderSn(req.OrderSn)
		if err != nil {
			middlewares.Logger.Println("RefundFromHis FindByOrderNo  failed ", err)
			return err
		}
		if !has {
			return errors.New("订单不存在")
		}
		if orders.Status != "confirm" {
			return errors.New("订单状态错误")
		}
		// 只退钱
		//f, err := strconv.ParseFloat(req.Fee, 64)
		if err != nil {
			fmt.Println("转换失败：", err)
			return errors.New("订单金额转换失败")
		}
		refundReq := &models.RefundReq{
			Id:         constant.MerchantId,
			ReturnCode: "refund_sn",
			OrderSn:    req.OrderSn,
			RefundFee:  req.Fee,
			BusiType:   "reg",
		}
		resp, err := (&PaymentService{}).Refund(refundReq)
		if !resp.IsSuccess {
			return errors.New(resp.Msg)
		}
		if resp.Data.ReturnCode != "000000" {
			return errors.New(resp.Data.ReturnMsg)
		}
		orders.PayStatus = "refunded-his"
		orders.RefundSn = resp.Data.CustId
		orders.Remark = "窗口退费"
		err = orders.UpdateRefund()
		if err != nil {
			middlewares.Logger.Println("reg UpdateRefund failed", orders.OrderSn, err)
		}

		hrm := &models.RefundModel{
			OrderSn:   req.OrderSn,
			RefundSn:  resp.Data.CustId,
			RefundFee: resp.Data.TxInfo.Amount,
			Remark:    "窗口退费",
		}
		err = hrm.Create()
		if err != nil {
			middlewares.Logger.Println("reg HisRefundModel create failed", orders.OrderSn, err)
		}
		return nil
	}
	// 门诊退费
	if strings.Contains(req.OrderSn, "P") {
		reqHis := &models.PrescriptionModel{}
		orders, has, err := reqHis.FindByOrderSn(req.OrderSn)
		if err != nil {
			middlewares.Logger.Println("RefundFromHis FindByOrderNo  failed ", err)
			return err
		}
		if !has {
			return errors.New("订单不存在")
		}
		//	return errors.New("订单支付状态错误")
		//}
		// 只退钱
		//f, err := strconv.ParseFloat(req.Fee, 64)
		if err != nil {
			fmt.Println("转换失败：", err)
			return errors.New("订单金额转换失败")
		}
		refundReq := &models.RefundReq{
			Id:         constant.MerchantId,
			ReturnCode: "refund_sn",
			OrderSn:    req.OrderSn,
			RefundFee:  req.Fee,
			BusiType:   "op",
		}
		resp, err := (&PaymentService{}).Refund(refundReq)
		if !resp.IsSuccess {
			return errors.New(resp.Msg)
		}
		if resp.Data.ReturnCode != "000000" {
			return errors.New(resp.Data.ReturnMsg)
		}
		opOrder := &models.PrescriptionModel{
			OrderSn:   req.OrderSn,
			PayStatus: "refunded-his",
			RefundSn:  resp.Data.CustId,
			Remark:    "窗口退费",
		}
		orders.PayStatus = "refunded-his"
		orders.RefundSn = resp.Data.CustId
		orders.Remark = "窗口退费"
		err = opOrder.UpdateRefund(opOrder)
		if err != nil {
			middlewares.Logger.Println("op UpdateRefund failed", orders.OrderSn, err)
		}

		hrm := &models.RefundModel{
			OrderSn:   req.OrderSn,
			RefundSn:  resp.Data.CustId,
			RefundFee: resp.Data.TxInfo.Amount,
			Remark:    "窗口退费",
		}
		err = hrm.Create()
		if err != nil {
			middlewares.Logger.Println("op HisRefundModel create failed", orders.OrderSn, err)
		}
		return nil
	}
	if strings.Contains(req.OrderSn, "YKT") {
		yktOrder, err := (&models.YktOrdersModel{}).FindByOrderSn(req.OrderSn)
		middlewares.Logger.Println(yktOrder)
		reqYkt := &models.YktRefundReq{
			MchOrderId:   req.OrderSn,
			PayOrderId:   yktOrder.PayOrderId,
			RefundAmount: req.Fee * 100,
			MchRefundId:  "CDGWYKTREFUND" + yktOrder.PayOrderId,
			RefundReason: "窗口退费",
		}
		jsonData, err := json.Marshal(reqYkt)
		_, err = (&YktService{}).Refund(string(jsonData), "88800100009")
		if err != nil {
			return err
		}
		return nil
	}
	return errors.New("订单类型错误")
}
